This example is for the ENUM constraints. The table in SQL can be created by $(D $(D sql CREATE TABLE Shops ( Id INTEGER NOT NULL PRIMARY KEY, Name VARCHAR(55), Quality ENUM(igh verage ow ); ))
1 import db_constraints; 2 3 class Shop 4 { 5 private int _Id; 6 @PrimaryKeyColumn @NotNull 7 @property int Id() 8 { 9 return _Id; 10 } 11 @property void Id(int value) 12 { 13 setter(_Id, value); 14 } 15 private string _Name; 16 @property string Name() 17 { 18 return _Name; 19 } 20 @property void Name(string value) 21 { 22 setter(_Name, value); 23 } 24 private string _Quality; 25 // Quality can only have a value that 26 // is among the enumeration below. 27 // Using false so this will not throw an 28 // exception but instead just change _Quality 29 // to an empty string. 30 @EnumConstraint!(false, "High", "Average", "Low") 31 @property string Quality() 32 { 33 return _Quality; 34 } 35 @property void Quality(string value) 36 { 37 setter(_Quality, value); 38 } 39 this(int Id_, string Name_, string Quality_) 40 { 41 this._Id = Id_; 42 this._Name = Name_; 43 this._Quality = Quality_; 44 initializeKeyedItem(); 45 } 46 47 mixin KeyedItem!(); 48 } 49 50 auto Boneys = new Shop(1, "Boneys", "High"); 51 assert(Boneys.Quality == "High"); 52 53 auto ACRiver = new Shop(2, "AC River", "Average"); 54 assert(ACRiver.Quality == "Average"); 55 56 auto AT34 = new Shop(3, "AT 34", "**"); 57 // since we have this as a non-strict enum, the 58 // quality is set to an empty string when 59 // given a string that is not in the enumeration 60 assert(AT34.Quality == "");